package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.repository.UserInfoRepository;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.Consts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Service
public class UserInfoRepositoryImpl implements IUserInfoService {

    @Autowired
    private UserInfoRepository userInfoRepository;

    @Override
    public void insert(UserInfo userInfo) {
        userInfoRepository.insert(userInfo);
    }

    @Override
    public void delete(String id) {
        userInfoRepository.deleteById(id);
    }

    @Override
    public void update(UserInfo userInfo) {
        userInfoRepository.save(userInfo);
    }

    @Override
    public UserInfo selectOne(String id) {
        UserInfo userInfo = userInfoRepository.findById(id).orElse(null);
        //UserInfo userInfo = byId.get();
        return userInfo;
    }

    @Override
    public List<UserInfo> selectAll() {
        List<UserInfo> all = userInfoRepository.findAll();
        return all;
    }

    @Override
    public boolean checkPhone(String phone) {
        //通过电话号码为查询条件，看数据中是否有该用户
        UserInfo userInfo=userInfoRepository.findByPhone(phone);
        return userInfo!=null;
    }

    @Override
    public String getCode() {
        String code1 = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 4);
        //模拟用信息发送验证码
        StringBuilder sb=new StringBuilder();
        sb.append("请您在").append(Consts.VERIFY_CODE_VAI_TIME).append("分钟之内使用").append(code1).append("验证码");
        System.out.println(sb);
        return code1;
    }
}
